﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.IO;

namespace TestMonkey.Core
{
    public class Setup
    {

        Func<Type, string, FileInfo> m_TargetFile;
        readonly ObservableCollection<string> m_SearchPaths = new ObservableCollection<string>();

        readonly ObservableCollection<string> m_TemplatePaths = new ObservableCollection<string>();

        public string SourceDll { get; set; }

        public string TestBaseClass = "";

        public ObservableCollection<string> SearchPaths
        {
            get { return m_SearchPaths; }
        }

        public ObservableCollection<string> TemplatePaths
        {
            get { return m_TemplatePaths; }
        }

        public string TestNamespace { get; set; }

        public string TargetFolder { get; set; }

        public string NamespaceRoot { get; set; }

        public Func<Type, string, FileInfo> TargetFile
        {
            get { return m_TargetFile; }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value", "value is null.");
                m_TargetFile = value;
            }
        }


        public FileInfo GetTargetFile(Type type, string part)
        {
            string temp = GetTestClassName(type);
            foreach (var c in Path.GetInvalidFileNameChars())
            {
                temp = temp.Replace(c, '_');
            }

            var partialNamespace = type.Namespace;
            if (partialNamespace.StartsWith(NamespaceRoot))
                partialNamespace = partialNamespace.Substring(NamespaceRoot.Length);
            partialNamespace = partialNamespace.Replace('.', Path.DirectorySeparatorChar);
            if (partialNamespace.StartsWith(Path.DirectorySeparatorChar.ToString()) || partialNamespace.StartsWith(Path.AltDirectorySeparatorChar.ToString()))
                partialNamespace = partialNamespace.Substring(1);

            return new FileInfo(Path.Combine(TargetFolder, partialNamespace, temp + "_" + part + ".cs"));
        }

        public string GetTestClassName(Type type)
        {
            return type.Name + "Tests";
        }

        public bool UseTags { get; set; }

    }


}


